home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / newmat03.lha / newmat03 / newmatrm.hxx < prev    next >
Text File  |  1993-08-08  |  4KB  |  103 lines

  1. //$$newmatrm.hxx                          rectangular matrix operations
  2.  
  3. // Copyright (C) 1991: R B Davies and DSIR
  4.  
  5. #ifndef NEWMATRM_LIB
  6. #define NEWMATRM_LIB 0
  7.  
  8.  
  9. // operations on rectangular matrices
  10.  
  11. class RectMatrixCol;
  12.  
  13. class RectMatrixRowCol
  14. // a class for accessing rows and columns of rectangular matrices
  15. {
  16. protected:
  17.    real* store;                   // pointer to storage
  18.    int n;                         // number of elements
  19.    int spacing;                   // space between elements
  20.    int shift;                     // space between cols or rows
  21.    RectMatrixRowCol(real* st, int nx, int sp, int sh)
  22.       : store(st), n(nx), spacing(sp), shift(sh) {}
  23.    void Reset(real* st, int nx, int sp, int sh)
  24.       { store=st; n=nx; spacing=sp; shift=sh; }
  25. public:
  26.    real operator*(const RectMatrixRowCol&) const;         // dot product
  27.    void AddScaled(const RectMatrixRowCol&, real);         // add scaled
  28.    void Divide(const RectMatrixRowCol&, real);            // scaling
  29.    void Divide(real);                                     // scaling
  30.    void Negate();                                         // change sign
  31.    void Zero();                                           // zero row col
  32.    real& operator[](int i) { return *(store+i*spacing); } // element
  33.    real SumSquare() const;                                // sum of squares
  34.    real& First() { return *store; }                       // get first element
  35.    void DownDiag() { store += (shift+spacing); n--; }
  36.    void UpDiag() { store -= (shift+spacing); n++; }
  37.    friend void ComplexScale(RectMatrixCol&, RectMatrixCol&, real, real);
  38.    friend void Rotate(RectMatrixCol&, RectMatrixCol&, real, real);
  39. };
  40.  
  41. class RectMatrixRow : public RectMatrixRowCol
  42. {
  43. public:
  44.    RectMatrixRow(const Matrix&, int, int, int);
  45.    RectMatrixRow(const Matrix&, int);
  46.    void Reset(const Matrix&, int, int, int);
  47.    void Reset(const Matrix&, int);
  48.    real& operator[](int i) { return *(store+i); }
  49.    void Down() { store += shift; }
  50.    void Right() { store++; n--; }
  51.    void Up() { store -= shift; }
  52.    void Left() { store--; n++; }
  53. };
  54.  
  55. class RectMatrixCol : public RectMatrixRowCol
  56. {
  57. public:
  58.    RectMatrixCol(const Matrix&, int, int, int);
  59.    RectMatrixCol(const Matrix&, int);
  60.    void Reset(const Matrix&, int, int, int);
  61.    void Reset(const Matrix&, int);
  62.    void Down() { store += spacing; n--; }
  63.    void Right() { store++; }
  64.    void Up() { store -= spacing; n++; }
  65.    void Left() { store--; }
  66.    friend void ComplexScale(RectMatrixCol&, RectMatrixCol&, real, real);
  67.    friend void Rotate(RectMatrixCol&, RectMatrixCol&, real, real);
  68. };
  69.  
  70. class RectMatrixDiag : public RectMatrixRowCol
  71. {
  72. public:
  73.    RectMatrixDiag(const DiagonalMatrix& D)
  74.       : RectMatrixRowCol(D.Store(), D.Nrows(), 1, 1) {}
  75.    real& operator[](int i) { return *(store+i); }
  76.    void DownDiag() { store++; n--; }
  77.    void UpDiag() { store--; n++; }
  78. };
  79.  
  80.  
  81. inline RectMatrixRow::RectMatrixRow
  82.    (const Matrix& M, int row, int skip, int length)
  83.    : RectMatrixRowCol( M.Store()+row*M.Ncols()+skip, length, 1, M.Ncols() ) {}
  84.  
  85. inline RectMatrixRow::RectMatrixRow (const Matrix& M, int row)
  86.    : RectMatrixRowCol( M.Store()+row*M.Ncols(), M.Ncols(), 1, M.Ncols() ) {}
  87.  
  88. inline RectMatrixCol::RectMatrixCol
  89.    (const Matrix& M, int skip, int col, int length)
  90.    : RectMatrixRowCol( M.Store()+col+skip*M.Ncols(), length, M.Ncols(), 1 ) {}
  91.  
  92. inline RectMatrixCol::RectMatrixCol (const Matrix& M, int col)
  93.    : RectMatrixRowCol( M.Store()+col, M.Nrows(), M.Ncols(), 1 ) {}
  94.  
  95. inline real square(real x) { return x*x; }
  96. inline real sign(real x, real y)
  97.    { return (y>=0) ? x : -x; }                    // assume x >=0
  98.  
  99.  
  100.  
  101.  
  102. #endif
  103.